﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CTMOES;
using CTMOES.Models;
using CTMAdmin;

namespace CTMOES.Helper
{
    public class DataHelper
    {
        private static readonly CTMDBEntities _db = new CTMDBEntities();

        public static List<OptionModel> GetOptions(Guid QueID)
        {
            List<Options> options = _db.Options.Where(p => p.QueID == QueID).ToList();
            List<OptionModel> optionList = new List<OptionModel>();
            OptionModel tmp;
            foreach (var op in options)
            {
                tmp = new OptionModel();
                tmp.ID = op.ID;
                tmp.QueID = Guid.Parse(op.QueID.ToString());
                tmp.Description = op.Description;
                optionList.Add(tmp);
            }
            return optionList;
        }

        public static List<QueModel> GetQueList(int Category)
        {
            var questions = _db.Question.Where(q => q.Category == Category).ToList();
            List<QueModel> QueList = new List<QueModel>();
            QueModel tmp;
            foreach (var question in questions)
            {
                tmp = new QueModel();
                tmp.ID = question.ID;
                tmp.Category = Convert.ToInt32(question.Category);
                tmp.ProContent = question.ProContent;
                tmp.OptionList = GetOptions(tmp.ID);
                QueList.Add(tmp);
            }
            return QueList;
        }
        public static List<Subject> AllSubject()
        {
            return _db.Subject.ToList();
        }
        public static void AddQue(QueModel que)
        {
            CTMAdmin.Question question=new Question();            
            question.ID = que.ID;
            question.Subject = que.SubjectID;
            question.ProContent = que.ProContent;
            question.Category = que.Category;
            _db.Question.AddObject(question);
            _db.SaveChanges();
        }
        public static void DelQue(Guid QueID)
        {
            var QueToDelete = _db.Question.Where(p => p.ID == QueID);
            var OpToDelete = _db.Options.Where(p => p.QueID == QueID);
            foreach (var question in QueToDelete)
            {
                _db.Question.DeleteObject(question);
            }
            foreach (var optionse in OpToDelete)
            {
                _db.Options.DeleteObject(optionse);
            }
            _db.SaveChanges();        
        }
        public static void AddOp(OptionModel op)
        {
            CTMAdmin.Options options=new Options();
            options.ID = op.ID;
            options.QueID = op.QueID;
            options.Description = op.Description;
            options.Correct = op.Correct;
            _db.Options.AddObject(options);
            _db.SaveChanges();
        }
        public static void EditQue(QueModel Que)
        {
            var tmp = _db.Question.Where(p => p.ID == Que.ID).FirstOrDefault();
            tmp.ProContent = Que.ProContent;
            tmp.Subject = Que.SubjectID;   
            
            _db.SaveChanges();
            
        }
        public static void EditOption(OptionModel Op)
        {
            var tmp = _db.Options.Where(p => p.ID == Op.ID).FirstOrDefault();
            tmp.Description = Op.Description;
            tmp.Correct = Op.Correct;
            _db.SaveChanges();
        }
        public static List<CTMAdmin.Judgement> GetAllJudgment()
        {
            CTMAdmin.Question que;
            var tmp = _db.Judgement.ToList();
            return tmp;
        }
    }

}